package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;
import com.yalantis.phoenix.PullToRefreshView;

import java.text.SimpleDateFormat;
import java.util.Date;

public class MainActivity extends AppCompatActivity {
    //https://www.sioe.cn/yingyong/yanse-rgb-16/ 颜色码
    //onResume() 在 Activity 从 Pause 状态转换到 Active 状态时被调用。在这个方法中访问SQLite数据库，通过Adapter将条目的内容和添加时间显示在ListView中。看代码：
    private Button mButton;
    private ListView mList;
    private Intent mIntent;
    private MyAdapter mAdapter;
    private MyDB mNotedb;
    private Cursor cursor;
    private SQLiteDatabase dbreader;

    private int white = 0xFFFFFFFF;
    private int gray = 0xFF7597B3;
    private int dark = 0xff000000;
    // 定义FragmentManager对象管理器
    private FragmentManager fragmentManager;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//Remove notification bar
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //以上两行代码是取消标题和黑框
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        PullToRefreshView mPullToRefreshView = (PullToRefreshView) findViewById(R.id.pull_to_refresh);
        mPullToRefreshView.setOnRefreshListener(new PullToRefreshView.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mPullToRefreshView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        System.out.println("....................................发生肾么事了");//在这加入刷新代码
                        mPullToRefreshView.setRefreshing(false);
                    }
                }, 1000);
            }
        });//以上为下滑刷新

        mList = (ListView) this.findViewById(R.id.list);
        mNotedb = new MyDB(this);
        dbreader = mNotedb.getReadableDatabase();
        mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {//跳转到指定的列
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                cursor.moveToPosition(i);
                Intent intent = new Intent(MainActivity.this,ShowContent.class);
                intent.putExtra(MyDB.ID,cursor.getInt(cursor.getColumnIndex(MyDB.ID)));
                intent.putExtra(MyDB.CONTENT,cursor.getString(cursor.getColumnIndex(MyDB.CONTENT)));
                intent.putExtra(MyDB.TIME,cursor.getString(cursor.getColumnIndex(MyDB.TIME)));
                intent.putExtra(MyDB.LOCATION,cursor.getString(cursor.getColumnIndex(MyDB.LOCATION)));
                startActivity(intent);
            }
        });


    }

    public void addClick(View v) {//点击增加新的记事本
        mIntent = new Intent(MainActivity.this,AddContent.class);
        startActivity(mIntent);
    }
    public void selectDb() {
        cursor = dbreader.query
                (MyDB.TABLE_NAME,null,null,null,null,null,null);
        mAdapter = new MyAdapter(this,cursor);
        mList.setAdapter(mAdapter);
    }

    @Override
    protected void onResume() {
        super.onResume();
        selectDb();
    }


}